package com.all.play.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.myapplication.Bean.RvDataBean;
import com.all.play.myapplication.R;
import com.all.play.myapplication.activity.DatabaseManActivity;
import com.all.play.myapplication.activity.ProductManagerActivity;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/4/18.
 * Time: 2018/4/18  15:57
 */

public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.MyViewHolder> {
    private Context mContext;
    private List<RvDataBean> rvlist;

    public MyRvAdapter(Context mContext, List<RvDataBean> rvlist) {

        this.mContext = mContext;
        this.rvlist = rvlist;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootview=View.inflate(mContext, R.layout.item_rv_show,null);
        return new MyViewHolder(rootview);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.setData(position);
        RvDataBean rvDataBean = rvlist.get(position);
        Glide.with(mContext).load(rvDataBean.getImage()).into(holder.image);

        holder.tv_name.setText(rvDataBean.getName());

    }

    @Override
    public int getItemCount() {
        return rvlist==null?0:rvlist.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder{

        ImageView image;
        TextView tv_name;

        public MyViewHolder(View itemView) {
            super(itemView);
            image= (ImageView) itemView.findViewById(R.id.image_show);
            tv_name= (TextView) itemView.findViewById(R.id.tv_name);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position= (int) view.getTag();

                    switch(position){
                        case 0:
                            mContext.startActivity(new Intent(mContext, ProductManagerActivity.class));
                            Toast.makeText(mContext, "在内存中进行增删改查!", Toast.LENGTH_SHORT).show();
                            break;

                        case 1:
                            mContext.startActivity(new Intent(mContext, DatabaseManActivity.class));
                            Toast.makeText(mContext, "在数据库中进行增删改查", Toast.LENGTH_SHORT).show();
                            break;

                        default:
                            Toast.makeText(mContext, "点击其他条目!", Toast.LENGTH_SHORT).show();
                            break;
                    }


                }
            });
        }

        public void setData(int position){

            itemView.setTag(position);


        }
    }
}
